<?php
/**
 * Created by PhpStorm
 * User: whh
 * Date: 2024/5/18
 * Time: 21:11
 */

namespace app\reception\controller;


use think\Controller;
use think\Db;

class Link extends Controller
{
    public function open(){
        $token = input('token');
        $adtype = input('adtype');
        $request_id = input('request_id');
        $req_id = input('req_id');
        if (!$token){
            echo "参数异常";die;
        }
        $link = Db::table("quick_link")->where("token",$token)->field("id,quick_id,status")->find();
        if (!$link){
            echo "链接不存在";die;
        }
        if ($link['status'] != 1 || !$this->check_isMobile()){
            header("Location: http://ldy.yxwlaqq.cn");die;
        }
        $package = Db::table("quick_app")->where("id",$link['quick_id'])->cache(3600)->value("package_name");
        $this->assign('token',$token);
        $this->assign('adtype',$adtype);
        $this->assign('request_id',$request_id);
        $this->assign('req_id',$req_id);
        $this->assign('package',$package);
        return $this->fetch();
    }


    /**
     * 判断是否为手机
     * @return bool
     */
    private function check_isMobile()
    {
        if (!$this->isMobile()){
            return false;
        }
        if (!preg_match('/Mobile|Android|Silk\/|Kindle|BlackBerry|Opera Mini|Opera Mobi/', $_SERVER['HTTP_USER_AGENT'])){
            return false;
        }
        $mobileDevices = array(
            'android',
            'blackberry',
            'iphone',
            'ipad',
            'ipod',
            'kindle',
            'linux',
            'mac',
            'palm',
            'psp',
            'smartphone',
            'symbian',
            'tablet',
            'up.browser',
            'up.link',
            'windows ce',
            'windows phone'
        );
        $userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
        // 检查是否包含移动设备的关键字
        foreach ($mobileDevices as $device) {
            if (strstr($userAgent, $device)) {
                return true;
            }
        }
        return false;
    }


    private function isMobile()
    {
        if (isset($_SERVER['HTTP_X_WAP_PROFILE'])) {
            return true;
        }

        if (isset($_SERVER['HTTP_VIA'])) {
            return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
        }

        if (isset($_SERVER['HTTP_USER_AGENT'])) {
            $clientkeywords = array('nokia', 'sony', 'ericsson', 'mot', 'samsung', 'htc', 'sgh', 'lg', 'sharp', 'sie-', 'philips', 'panasonic', 'alcatel', 'lenovo', 'iphone', 'ipod', 'blackberry', 'meizu', 'android', 'netfront', 'symbian', 'ucweb', 'windowsce', 'palm', 'operamini', 'operamobi', 'openwave', 'nexusone', 'cldc', 'midp', 'wap', 'mobile', 'MicroMessenger');

            if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
                return true;
            }
        }

        if (isset ($_SERVER['HTTP_ACCEPT'])) {
            if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) {
                return true;
            }
        }
        return false;
    }

}